from sqlalchemy import select

from poker_bot.core.interfaces import PaymentMethodReader
from poker_bot.core.models.dto import PaymentDetailsDTO
from poker_bot.core.models.enums.currency import Currency
from poker_bot.database.converters.payment_details import (
    convert_db_model_to_payment_details_dto,
)
from poker_bot.database.exception_mapper import exception_mapper
from poker_bot.database.models import PaymentDetails
from poker_bot.database.repositories.base import SQLAlchemyRepo


class PaymentMethodReaderImpl(SQLAlchemyRepo, PaymentMethodReader):
    @exception_mapper
    async def get_payment_method_by_id(
        self, payment_method_id: int
    ) -> PaymentDetailsDTO | None:
        """Получаем метод оплаты по id"""

        payment_method: PaymentDetails | None = await self._session.scalar(
            select(PaymentDetails).where(
                PaymentDetails.id == payment_method_id,
            )
        )
        if not payment_method:
            return None

        return convert_db_model_to_payment_details_dto(payment_method)

    @exception_mapper
    async def get_payment_method_by_currency(
        self, currency: Currency
    ) -> PaymentDetailsDTO:
        """Получаем метод оплаты по валюте"""

        payment_method: PaymentDetails | None = await self._session.scalar(
            select(PaymentDetails).where(
                PaymentDetails.currency == currency,
            )
        )

        payment_method_dto = convert_db_model_to_payment_details_dto(payment_method)

        return payment_method_dto

    @exception_mapper
    async def get_payment_methods(self) -> list[PaymentDetailsDTO]:
        """Получаем все методы оплаты"""

        res = await self._session.scalars(select(PaymentDetails))
        payment_methods: list[PaymentDetails] = list(res)

        payment_methods_dtos = [
            convert_db_model_to_payment_details_dto(payment_method)
            for payment_method in payment_methods
        ]

        return payment_methods_dtos
